import { Component, Input, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { DomSanitizer } from '@angular/platform-browser';

declare var security: any;

@Component({
  selector: 'app-bpm-image',
  templateUrl: './bpm.image.component.html'
})
export class BpmImageComponent implements OnInit {

  imageSrc: any = null;

  bs: any = null;
  @Input()
  get bpmServer() {
    return this.bs;
  }
  set bpmServer(value) {
    this.bs = value;
  }

  constructor(private http: HttpClient, private domSanitizer: DomSanitizer) {

  }

  ngOnInit() {
    const jwts = security.SecurityService.jwt;
    this.http.get(this.bs.imageUrl, {
      headers: new HttpHeaders({ jwt: jwts }),
      responseType: 'blob', observe: 'response'
    }).subscribe(rs => {
      const pdfFile = new Blob([rs.body], { type: 'image/png' });
      const datalocalURL = window.URL.createObjectURL(pdfFile);
      this.imageSrc = this.domSanitizer.bypassSecurityTrustResourceUrl(datalocalURL);
    });
  }
}
